package com.example.iitematicviewer.api.login;

import com.alibaba.fastjson.JSONObject;
import com.example.iitematicviewer.constant.ExceptionConstant;
import com.example.iitematicviewer.mapper.CustomDao;
import com.example.iitematicviewer.mapper.LoginDao;
import com.example.iitematicviewer.util.RSAUtil;
import com.example.iitematicviewer.util.SnowFlakeGenerateIdWorker;
import com.example.iitematicviewer.vo.CustomVO;
import com.example.iitematicviewer.vo.ResultVO;
import com.example.iitematicviewer.vo.UserVO;
import org.apache.catalina.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

@Service
public class LoginTransService {

    @Autowired
    private CustomDao dao;

    @Autowired
    private LoginDao loginDao;

    /**
     * 登录判空方法
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO judgingEmptinessByLogin(JSONObject param,ResultVO resultVO){
        //账号(暂存防止返工)
        /*String user = param.getString(LoginConstant.user);
        if (StringUtils.isEmpty(user)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.mustUser);
            return resultVO;
        }*/
        //密码
        String password = param.getString(LoginConstant.password);
        if (StringUtils.isEmpty(password)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.mustPassword);
            return resultVO;
        }
        return resultVO;
    }

    /**
     * 将JSON转换为用户实体
     * @return
     */
    public UserVO transUserVOByJSON(JSONObject param,ResultVO resultVO){
        UserVO userVO = new UserVO();
        //主键
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker = new SnowFlakeGenerateIdWorker(0L,0L);
        String id = snowFlakeGenerateIdWorker.generateNextId();
        userVO.setId(id);
        //账号(暂存防止需返工)
        /*String user = param.getString(LoginConstant.user);
        if (StringUtils.isNotEmpty(user)) {
            userVO.setUser(user);
        }*/
        userVO.setUser(randomUser());
        //密码(加密)
        String password = param.getString(LoginConstant.password);
        CustomVO publicKey = dao.selectOneByCode(LoginConstant.customByPublicKey);
        if (null != publicKey){
            String newPassword = RSAUtil.encryptAndEncode(password, publicKey.getContent());
            userVO.setPassword(newPassword);
        }
        //状态
        userVO.setStatus(0);
        //权限
        userVO.setGrade(0);
        //交互次数
        userVO.setBotNum(0);
        //创建时间
        userVO.setCreateTime(getNowDate());
        //修改时间
        userVO.setUpdateTime(getNowDate());
        //免费硬币
        userVO.setFreeConis(0);
        //充值硬币
        userVO.setRechargeConis(0);
        //积分数量
        userVO.setIntegral(0);
        //总点赞
        userVO.setLikeNum(0);
        //总关注
        userVO.setFollow(0);
        //总收藏
        userVO.setCollect(0);
        //总动态
        userVO.setTrends(0);
        //总回复数量
        userVO.setReplyNum(0);
        //总解决问题数量
        userVO.setSolveNum(0);
        return userVO;
    }

    /**
     * 密码解密方法
     * @param password
     * @return
     */
    public ResultVO encryption(UserVO userVO, ResultVO resultVO, String password, String newPassword){
        CustomVO customVO = dao.selectOneByCode(LoginConstant.customByPrivateKey);
        if (null != customVO){
            password = RSAUtil.decryptAndDecode(password, customVO.getContent());
            if (!password.equals(newPassword)) {
                resultVO.setCode(ExceptionConstant.five);
                resultVO.setMessage(LoginExceptionConstant.badLoginByPassword);
                userVO.setBotNum(userVO.getBotNum() + 1);
                loginDao.updateByFreeze(userVO);
            }
            else {
                resultVO.setMessage(LoginExceptionConstant.login);
            }
        }
        return resultVO;
    }

    /**
     * 获取当前时间
     * @return
     */
    private String getNowDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd : hh:mm:ss");
        return dateFormat.format(date);
    }

    /**
     * 生成随机账号
     * @return
     */
    public String randomUser(){
        //前三位
        Integer firstUser = firstUser();
        //后五位
        Integer lastUser = lastUser();
        //合并
        return combineUser(firstUser,lastUser);
    }

    /**
     * 账号前三位
     * @return
     */
    public Integer firstUser(){
        CustomVO customVO = dao.selectOneByCode(LoginConstant.user);
        Integer firstUser = 0;
        if (null != customVO){
            if (StringUtils.isNotEmpty(customVO.getContent())) {
                firstUser = Integer.parseInt(customVO.getContent());
            }
        }
        return firstUser;
    }

    /**
     * 随机账号后五位
     * @return
     */
    public synchronized Integer lastUser(){
        Random random = new Random();
        int lastUser = random.nextInt(99999);
        //修改随机生成次数
        CustomVO customVO = dao.selectOneByCode(LoginConstant.customByUserSum);
        if (StringUtils.isNotEmpty(customVO.getContent())) {
            Integer num = Integer.parseInt(customVO.getContent());
            if (num == 3000){
                customVO.setContent("0");
                CustomVO userNum = dao.selectOneByCode(LoginConstant.user);
                if (StringUtils.isNotEmpty(userNum.getContent())) {
                    Integer oderNum = Integer.parseInt(userNum.getContent());
                    oderNum += 1;
                    userNum.setContent(oderNum.toString());
                    dao.update(userNum);
                }
            }
            else{
                num += 1;
                customVO.setContent(num.toString());
            }
            dao.update(customVO);
        }
        return lastUser;
    }

    /**
     * 合并随机账号
     * @param first
     * @param last
     * @return
     */
    public String combineUser(Integer first,Integer last){
        return first + last.toString();
    }
}
